package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotIndustryService;
import com.lechi.web.system.domain.IotIndustry;
import com.lechi.web.system.mapper.iot.IotIndustryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 行业分类Service业务层处理
 *
 * @author yangguang
 * @date 2024 -05-17
 */
@Service
@RequiredArgsConstructor
public class IotIndustryServiceImpl implements IIotIndustryService {

    private final IotIndustryMapper iotIndustryMapper;

    /**
     * 查询行业分类
     *
     * @param id 行业分类主键
     * @return 行业分类
     */
    @Override
    public IotIndustry selectIotIndustryById(Long id) {
        return iotIndustryMapper.selectIotIndustryById(id);
    }

    /**
     * 查询行业分类列表
     *
     * @param iotIndustry 行业分类
     * @return 行业分类
     */
    @Override
    public List<IotIndustry> selectIotIndustryList(IotIndustry iotIndustry) {
        try {
            return iotIndustryMapper.selectIotIndustryList(iotIndustry);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询行业分类列表", method, iotIndustry));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增行业分类
     *
     * @param iotIndustry 行业分类
     */
    @Override
    public void insertIotIndustry(IotIndustry iotIndustry) {
        iotIndustry.setCreateTime(new Date());
        try {
            iotIndustryMapper.insertIotIndustry(iotIndustry);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增行业分类", method, iotIndustry));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改行业分类
     *
     * @param iotIndustry 行业分类
     */
    @Override
    public void updateIotIndustry(IotIndustry iotIndustry) {
        iotIndustry.setUpdateTime(new Date());
        try {
            iotIndustryMapper.updateIotIndustry(iotIndustry);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改行业分类", method, iotIndustry));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 批量删除行业分类
     *
     * @param ids 需要删除的行业分类主键
     */
    @Override
    public void deleteIotIndustryByIds(Long[] ids) {
        try {
            iotIndustryMapper.deleteIotIndustryByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除行业分类", method, ids));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }
}
